package com.jiesen.base.modules.uploadFile.controller;


import com.jiesen.base.modules.uploadFile.entity.Result;
import com.jiesen.base.modules.uploadFile.model.FileAttributes;
import com.jiesen.base.modules.uploadFile.pojo.FIleAttributesFrom;
import com.jiesen.base.modules.uploadFile.service.FileService;
import com.jiesen.base.modules.uploadFile.utils.FileAttributesUtil;
import com.jiesen.base.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/upload")
public class FileUpLoadCotroller {

    @Autowired
    private FileService fileService;

    //直接上传至七牛云
    @PostMapping("qiniu")
    @ResponseBody
    public Result uploadFile1(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (!file.isEmpty()) {
            String name = request.getParameter("name");
            FileAttributes fileAttributes = FileAttributesUtil.multipartFileToFileAttributes(file, name);
            //上传者ip
            fileAttributes.setOperatorUrl(request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for"));
            //上传者
            fileAttributes.setOperator(request.getParameter("operator"));
            try {
                FIleAttributesFrom s = fileService.uploadToQiniu(fileAttributes);
                return Result.ok(200, s);
            } catch (Exception e) {
                System.out.println(e);
                return Result.error("文件上传失败");
            }
        } else {
            return Result.error("文件上传失败");
        }
    }

    //获取token
    @GetMapping("qiniuToken")
    @ResponseBody
    public Result qiniuToken() {
        return Result.ok(200, QiniuUtil.getToken());
    }

    //获取serverName
    @GetMapping("serverName")
    @ResponseBody
    public Result getServerName() {
        return Result.ok(200, FileAttributesUtil.newFileName());
    }

    //获取下载链接
    @GetMapping("/download/{id}")
    @ResponseBody
    public Result downloadFile(@PathVariable("id") Integer id) {
        FileAttributes fileAttributes = fileService.findById(id);
        FIleAttributesFrom s = new FIleAttributesFrom(fileAttributes);
        return Result.ok(200, s);
    }

    //查询所有
    @GetMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        List<FileAttributes> list = fileService.findAll();
        return Result.ok(0, list.size(), list);
    }

    //根据分类查询
    @GetMapping("/findType/{id}")
    @ResponseBody
    public Result findType(@PathVariable("id") Integer id) {
        List<FileAttributes> list = fileService.findByType(id);
        return Result.ok(list);
    }

    //根据文件夹id查询
    @GetMapping("/viewFolder/{id}")
    @ResponseBody
    public Result findByFolderId(@PathVariable("id") Integer id) {
        List<FileAttributes> list = fileService.findByFolderId(id);
        return Result.ok(list);
    }

    //查询单个文件信息
    @GetMapping("/view/{id}")
    @ResponseBody
    public Result findById(@PathVariable("id") Integer id) {
        FileAttributes fileAttributes = fileService.findById(id);
        return Result.ok(fileAttributes);
    }

    //修改提交
    @PostMapping("/edit/{id}")
    @ResponseBody
    public Result editById(@PathVariable("id") Integer id, HttpServletRequest request) {
        if (id != null) {
            FileAttributes fileAttributes = fileService.findById(id);
            String name = request.getParameter("name");
            Integer folderId = Integer.valueOf(request.getParameter("folderId"));
            if (name != null) {
                fileAttributes.setName(name);
            }
            if (folderId != null) {
                fileAttributes.setFolderId(folderId);
            }
            //修改ip
            fileAttributes.setUpdateOperatorUrl(request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for"));
            //修改者
            fileAttributes.setUpdateOperator(request.getParameter("operator"));
            //主键放入
            //fileAttributes.setId(id);
            try {
                fileService.updateById(fileAttributes);
            } catch (Exception e) {
                System.out.println(e);
                return Result.error("修改失败");
            }
        } else {
            return Result.error("修改失败");
        }
        return Result.ok("修改成功");
    }

    //删除数据
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id") Integer id) {
        int i = fileService.deleteById(id);
        return Result.ok("删除成功");
    }

    //将文件信息保存
    @PostMapping("/creace")
    @ResponseBody
    public Result saveFile(FileAttributes fileAttributes, HttpServletRequest request) {
        //收到有oldName||name,serverName,url,serverType,size,folderId;

        if (fileAttributes.getServerName() == null || fileAttributes.getSize() == null || fileAttributes.getFolderId() == null) {
            return Result.error("信息不完全，无法上传");
        }

        //上传者ip
        fileAttributes.setOperatorUrl(request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for"));
        //上传者
        fileAttributes.setOperator(request.getParameter("operator"));
        int i = fileService.InsertFileAttributes(fileAttributes);
        if(i>0) {
            return Result.ok(200,new FIleAttributesFrom(fileAttributes));
        }
        return Result.error("失败");
    }

    //测试文件上传controller
    @PostMapping("testUpload")
    @ResponseBody
    public Result uploadFile2(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        FileAttributes fileAttributes = null;
        if (!file.isEmpty()) {
            String name = request.getParameter("name");
            fileAttributes = FileAttributesUtil.multipartFileToFileAttributes(file, name);
            //上传者ip
            fileAttributes.setOperatorUrl(request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for"));
            //上传者
            fileAttributes.setUpdateOperator(request.getParameter("operator"));

            fileAttributes.setUrl("上传成功后，url也会返回的，在这个地方");
        } else {
            return Result.error("没有文件上传");
        }
        return Result.ok(fileAttributes);
    }

}
